package user

import (
	"MembershipRegist/db"
	"MembershipRegist/models"
)

func (c *Controller) SetPassword() {
	oldPassword := c.GetString("old_password")
	newPassword := c.GetString("new_password")
	rePassword := c.GetString("re_password")
	if oldPassword == "" || newPassword == "" || rePassword == "" {
		c.ResponseFailed(201, "参数不能为空")
		return
	}

	if c.User.Password != oldPassword {
		c.ResponseFailed(202, "密码错误")
		return
	}

	if newPassword != rePassword {
		c.ResponseFailed(203, "两次输入新密码不一致")
		return
	}

	db.DB.Model(&models.User{}).Where("id = ?", c.UserID).
		Update("password", newPassword)

	c.DelSession("user_id")
	c.ResponseSuccess()
}
